import React, { useState, useEffect } from 'react'
import { Form } from 'antd'
import { useRecoilValue } from 'recoil'
import { IHospital, IBasicMsg, IFilter, ITableParams, IWardList } from '../type/hospital'
import { IWardOption } from '../type'
import { getVariableById, getQuarter, getWard, getList, getCardList } from '../service/hospital'
import { isNullData, isNullArray } from '@/utils'
import { IYearList, IQuarterList } from '@/pages/DataFilling/type'
import { userInfo } from '@/store/userInfo'
import { CardList } from '@/@types'

export default (): IHospital => {
  // 用户信息
  const userMsg = useRecoilValue(userInfo)
  // 医院的基本数据信息
  const [basicMsg, setBasicMsg] = useState<IBasicMsg>()
  // 季度筛选框内容
  const [quarterOptions, setQuarterOptions] = useState<IYearList[]>([])
  // 保存所有病区
  const [allWard, setAllWard] = useState<IWardOption[]>([])
  // 筛选内容
  const [filterOptions, setFilterOptions] = useState<IFilter>({
    quarter: '',
    wordId: '',
    status: '',
    wardId: '',
  })
  // 病区上报列表
  const [wardReportList, setWardReportList] = useState<IWardList[]>([])
  // 病区头部
  const [wardHeadList, setWardHeadList] = useState<CardList[]>([])

  // 筛选表单
  const [filterForm] = Form.useForm()

  // 获取列表
  const getListHandle = async () => {
    const value = filterForm.getFieldsValue()
    const params = {
      dataReportYear: value?.quarter ? value?.quarter[0] : '',
      dataReportQuarter: value?.quarter ? value?.quarter[1] : '',
      wardId: value.wordId,
      hospitalCode: userMsg.hospitalCode,
      status: value.status !== '' ? value.status : '',
    }
    const res = await getList(params)
    if (isNullData(res) && res.data) {
      setWardReportList(res.data.hospitalWardBottomVOList)
      setWardHeadList(res.data.hospitalWardHeadVOList)
    }
  }

  const formChange = (changedValues: any, allValues: any) => {
    setFilterOptions(allValues)
  }
  const init = async () => {
    // hospitalId 用户详情中医院id
    const params = {
      hospitalCode: userMsg.hospitalCode,
    }
    const resBasic = await getVariableById(params)
    console.log(resBasic)
    if (isNullData(resBasic) && resBasic.data) {
      setBasicMsg(resBasic.data)
    }
    const resQuarter = await getQuarter()
    if (resQuarter.data) {
      setQuarterOptions(resQuarter.data)
      if (resQuarter.data[0]) {
        const initValue = [resQuarter.data[0].dataReportYear, resQuarter.data[0].childrenList[0].quarterId]
        filterForm.setFieldValue('quarter', initValue)
      }
      const resWard = await getWard({
        hospitalCode: userMsg.hospitalCode,
        dataReportYear: resQuarter.data[0]?.dataReportYear,
        dataReportQuarter: resQuarter.data[0]?.childrenList[0].quarterId,
      })
      if (resWard.data) setAllWard(resWard.data)
      filterForm.setFieldValue('wordId', 0)
      await getListHandle()
    }
  }

  // 季度选择回调
  const quarterSelect = async (value: any) => {
    const resWard = await getWard({
      hospitalCode: userMsg.hospitalCode,
      dataReportYear: value[0],
      dataReportQuarter: value[1],
    })
    if (resWard.data) setAllWard(resWard.data)
    filterForm.setFieldValue('wordId', 0)
    await getListHandle()
  }

  useEffect(() => {
    init()
  }, [])

  const tableParams: ITableParams = {
    wardReportList,
  }
  return {
    quarterSelect,
    basicMsg,
    filterForm,
    quarterOptions,
    allWard,
    formChange,
    tableParams,
    wardHeadList,
    getListHandle,
  }
}
